package com.ecoolex.ying.space.api.graphql;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.GoodsClassify;
import com.ecoolex.ying.space.pojo.QueryGoodsClassifyDTO;
import com.ecoolex.ying.space.ws.GoodsService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import java.util.List;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.User;

@GraphQL2Module(type = User, description = "商品分类管理")
public class GoodsClassifyGraphql {
    @Reference
    private GoodsService goodsService;
    @Autowired
    private StorageService storageService;

    @TypeField(type = GoodsClassify.class, field = "image", description = "图片")
    public StorageInfo image(@Source GoodsClassify source) {
        return storageService.get(source.getImage());
    }

    @TypeField(type = GoodsClassify.class, field = "parent", description = "父分类")
    public GoodsClassify parent(@Source GoodsClassify source) {
        if (Check.notNullAndGtZero(source.getParentClassifyId())) {
            return goodsService.getClassifyById(source.getParentClassifyId());
        } else {
            return null;
        }
    }

    @TypeField(type = GoodsClassify.class, field = "childList", description = "子分类列表")
    public List<GoodsClassify> childList(@Source GoodsClassify source) {
        return goodsService.queryClassifyByParentId(source.getClassifyId(), true);
    }

    @QueryField(field = "goodsClassify", description = "获取商品分类详情")
    public GoodsClassify get(@NonNull Integer classifyId, ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.getClassifyById(classifyId);
    }

    @QueryField(field = "goodsClassifyList", description = "查询商品分类列表")
    public Pagination<GoodsClassify> query(@NonNull QueryGoodsClassifyDTO query, @NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.queryClassify(query, sorts, page);
    }

    @QueryField(field = "goodsParentClassifyList", description = "查询商品父分类列表")
    public List<GoodsClassify> goodsParentClassifyList(ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.queryAllParentClassify(true);
    }

    @QueryField(field = "goodsClassifyListByParent", description = "根据父级分类查询商品分类列表")
    public List<GoodsClassify> goodsClassifyListByParent(@NonNull Integer parentClassifyId, ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.queryClassifyByParentId(parentClassifyId, true);
    }

    @QueryField(field = "goodsClassifyAllList", description = "查询所有商品分类列表")
    public List<GoodsClassify> goodsClassifyAllList(ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.queryAllClassify(true);
    }
}
